package com.vf.cloud.paas.app.master.api.render.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.jfinal.kit.JsonKit;
import com.jfinal.kit.StrKit;
import com.vf.cloud.paas.app.master.common.domain.RenderAgentSignalling;
import com.vf.cloud.paas.common.cirrus.Cirrus;
import com.vf.cloud.paas.common.datasource.annotation.Tx;
import com.vf.cloud.paas.common.log.annotation.RLog;
import com.vf.cloud.paas.common.security.annotation.TokenRequired;
import com.vf.cloud.paas.common.util.R;
import com.vf.cloud.paas.common.util.UuidUtil;

import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/render/agent/signalling")
public class RenderAgentSignallingController {

	
	@RLog(value = "保存信令服务", type = "user")
	@TokenRequired
	@Tx
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public R<RenderAgentSignalling> save(HttpServletRequest request,
			@RequestBody RenderAgentSignalling renderAgentSignalling) {
		try {
			if (StrKit.notBlank(renderAgentSignalling.getId())) {
				if (renderAgentSignalling.update()) {
					return R.ok(renderAgentSignalling);
				} else {
					return R.failed();
				}
			} else {
				renderAgentSignalling.setId(UuidUtil.getUUID());
				if (renderAgentSignalling.save()) {
					return R.ok(renderAgentSignalling);
				} else {
					return R.failed();
				}
			}
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}

	@RLog(value = "重启信令服务",type = "user")
	@TokenRequired
	@Tx
	@RequestMapping(value = "/syn", method = RequestMethod.POST)
	public R<String> restart(HttpServletRequest request, @RequestBody RenderAgentSignalling param) {
		try {
			RenderAgentSignalling renderAgentSignalling = RenderAgentSignalling.dao.findById(param.getId());
			if (renderAgentSignalling == null) {
				return R.failed();
			}
			ChannelHandlerContext ctx = Cirrus.AgentManager.get(renderAgentSignalling.getId());
			if (ctx == null) {
				return R.failed("节点掉线中,稍后再试.");
			}

			Map<String, Object> clientConfig = new HashMap<String, Object>();
			clientConfig.put("type", "SynForSignalling");
			clientConfig.put("signalling", renderAgentSignalling);
			String mm = JsonKit.toJson(clientConfig);

			ctx.channel().writeAndFlush(new TextWebSocketFrame(mm)).addListener(new ChannelFutureListener() {
				@Override
				public void operationComplete(ChannelFuture future) throws Exception {
					if (future.isSuccess()) {
						// log.info(String.format("-> Streamer:%s", mm));
					} else {
						// log.info(String.format("-> Streamer isError :%s", mm));
					}
				}
			});

			return R.ok();
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}

}
